package com.boot_reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.boot_reggie.Service.impl.OrdersDetailServiceImpl;
import com.boot_reggie.Service.impl.OrdersServiceImpl;
import com.boot_reggie.common.Result;
import com.boot_reggie.domain.Orders;
import com.boot_reggie.domain.OrdersDto;
import com.boot_reggie.domain.ShoppingCart;
import com.boot_reggie.domain.User;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersServiceImpl ordersService;

    @PostMapping
    public Result<Orders> save(@RequestBody Orders orders, HttpSession session, HttpServletRequest req, HttpServletResponse res){
        Orders num = new Orders();
        try{
            num.setId(ordersService.submit(orders,session,req,res));

        }catch (Exception e){
            e.printStackTrace();
        }
        LambdaQueryWrapper<Orders> lwq = new LambdaQueryWrapper<>();
        lwq.eq(Orders::getNumber,num.getId());
        Orders data = ordersService.getOne(lwq);
        return Result.success(data,"下单成功");
    }
    @GetMapping("/getOne")
    public Result<List<OrdersDto>> getOne(HttpSession session){
        User user = (User) session.getAttribute("userInfo");

        System.out.println("============"+user);
        List<OrdersDto> list = ordersService.getOrders(user);
        return Result.success(list,"123");
    }
    @GetMapping("Num")
    public Result<Integer> getNum(){
        LambdaQueryWrapper<Orders> lwq = new LambdaQueryWrapper<>();
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        //当月有多少天
        int maxDate = a.get(Calendar.DATE);

        SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-");
        String startTime = sdfTwo.format(new Date()) + "01 00:00:00";
        String endTime = sdfTwo.format(new Date()) + maxDate + " 23:59:59";
        System.out.println(startTime);
        System.out.println(endTime);
        lwq.between(Orders::getOrderTime,startTime,endTime);
        lwq.eq(Orders::getComment,1);
        Integer num = ordersService.count(lwq);
        return Result.success(num,"销量查询成功");
    }

    @GetMapping("page")
    public Result<IPage<Orders>> getAll(int page,int size){
        IPage<Orders> page1 = ordersService.getAll(page,size);
        return Result.success(page1,"数据获取成功");
    }

    @GetMapping("getShopping/{id}")
    public Result<OrdersDto> getShopping(@PathVariable Long id){

        return Result.success(ordersService.getShop(id),"查询成功");
    }

    @GetMapping("getPage")
    public Result<IPage<Orders>> getPage(String name,String time1,String time2,int page,int size){
        IPage page1 = ordersService.getOrders(name,time1,time2,page,size);
        return Result.success(page1,"123");
    }

    @PutMapping
    public Result<String> update(@RequestBody Orders orders){
        ordersService.updateById(orders);

        return Result.success("更新成功");
    }

    @GetMapping("/Dingin")
    public Result<List<Orders>> getDingin(){
        List<Orders> list = ordersService.getDingin();
        return Result.success(list,"待出餐获取成功");
    }
}
